﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DAMBAOCLPM
{
    public partial class frmHOME : Form
    {
        public frmHOME()
        {
            InitializeComponent();
        }

        private void frmHOME_Load(object sender, EventArgs e)
        {
            clsVariables.Height_TopMenu = menuStrip1.Height + toolStrip1.Height + tabMain.Height;
            clsVariables.Width_This = this.Width;
            clsVariables.Height_This = this.Height;
            try
            {
                clsConnection.CreateConnection();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi cấu hình.\nVui lòng kiểm tra cấu hình server.", "Cấu hình", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (!CheckExistForm("frmCauhinh"))
                {
                    frmCauhinh tt = new frmCauhinh();
                    tt.MdiParent = this;
                    tt.Show();
                }
                else ActiveChildForm("frmCauhinh");
            }
        }

        private void btnDonvi_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmDonvi"))
            {
                frmDonvi dv = new frmDonvi();
                dv.MdiParent = this;
                dv.Show();
            }
            else ActiveChildForm("frmDonvi");
        }

        private void btnThisinh_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmThisinh"))
            {
                frmThisinh ts = new frmThisinh();
                ts.MdiParent = this;
                ts.Show();
            }
            else ActiveChildForm("frmThisinh");
        }

        private void btnMonthi_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmMonthi"))
            {
                frmMonthi mt = new frmMonthi();
                mt.MdiParent = this;
                mt.Show();
            }
            else ActiveChildForm("frmMonthi");
        }

        private void btnDethi_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmDethi"))
            {
                frmDethi dt = new frmDethi();
                dt.MdiParent = this;
                dt.Show();
            }
            else ActiveChildForm("frmDethi");
        }

        private void btnXepphong_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmXepphong"))
            {
                frmXepphong xp = new frmXepphong();
                xp.MdiParent = this;
                xp.Show();
            }
            else ActiveChildForm("frmXepphong");
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmThem"))
            {
                frmThem them = new frmThem();
                them.MdiParent = this;
                them.Show();
            }
            else ActiveChildForm("frmThem");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmXoa"))
            {
                frmXoa xoa = new frmXoa();
                xoa.MdiParent = this;
                xoa.Show();
            }
            else ActiveChildForm("frmXoa");
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmCapnhat"))
            {
                frmCapnhat cn = new frmCapnhat();
                cn.MdiParent = this;
                cn.Show();
            }
            else ActiveChildForm("frmCapnhat");
        }

        private void btnKetquathi_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmKetquathi"))
            {
                frmKetquathi kq = new frmKetquathi();
                kq.MdiParent = this;
                kq.Show();
            }
            else ActiveChildForm("frmKetquathi");
        }

        private void btnDanhsachnopbai_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmDanhsachnopbai"))
            {
                frmDanhsachnopbai dsnb = new frmDanhsachnopbai();
                dsnb.MdiParent = this;
                dsnb.Show();
            }
            else ActiveChildForm("frmDanhsachnopbai");
        }

        private void btnDanhsachthisinh_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmDanhsachthisinh"))
            {
                frmDanhsachthisinh dsts = new frmDanhsachthisinh();
                dsts.MdiParent = this;
                dsts.Show();
            }
            else ActiveChildForm("frmDanhsachthisinh");
        }

        private void btnXemketquatonghop_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmXemketquatonghop"))
            {
                frmXemketquatonghop kqth = new frmXemketquatonghop();
                kqth.MdiParent = this;
                kqth.Show();
            }
            else ActiveChildForm("frmXemketquatonghop");
        }

        private void btnXemdanhsach_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmXemdanhsach"))
            {
                frmXemdanhsach xds = new frmXemdanhsach();
                xds.MdiParent = this;
                xds.Show();
            }
            else ActiveChildForm("frmXemdanhsach");
        }

        private void btnXembaithi_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmXembaithi"))
            {
                frmXembaithi xbt = new frmXembaithi();
                xbt.MdiParent = this;
                xbt.Show();
            }
            else ActiveChildForm("frmXembaithi");
        }

        private void btnSaoluuketqua_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmSaoluuketqua"))
            {
                frmSaoluuketqua sl = new frmSaoluuketqua();
                sl.MdiParent = this;
                sl.Show();
            }
            else ActiveChildForm("frmSaoluuketqua");
        }

        private void btnThongtin_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmTTin"))
            {
                frmTTin tt = new frmTTin();
                tt.MdiParent = this;
                tt.Show();
            }
            else ActiveChildForm("frmTTin");
        }

        private void btnHuongdan_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmHuongdan"))
            {
                frmHuongdan hd = new frmHuongdan();
                hd.MdiParent = this;
                hd.Show();
            }
            else ActiveChildForm("frmHuongdan");
        }
        //Hàm kiểm tra sự tồn tại của form.
        private bool CheckExistForm(string name)
        {
            bool check = false;
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == name)
                {
                    check = true;
                    break;
                }
            }
            return check;
        }
        //
        //Hàm kích hoạt hiển thị form.
        private void ActiveChildForm(string name)
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Name == name)
                {
                    frm.Activate();
                    break;
                }
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Top = 193 - 44;
            this.Left = 0;
            this.Width = 993;
            this.Height = 437;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmHOME_Resize(object sender, EventArgs e)
        {
            clsVariables.Height_TopMenu = menuStrip1.Height + toolStrip1.Height + tabMain.Height;
            clsVariables.Width_This = this.Width;
            clsVariables.Height_This = this.Height;
            foreach (Form frm in this.MdiChildren)
            {
                frm.Width = clsVariables.Width_This - 21;
                frm.Height = clsVariables.Height_This - clsVariables.Height_TopMenu - 65;
                frm.Top = clsVariables.Height_TopMenu - 66;
                frm.Left = 0;
            }

        }

        private void frmHOME_FormClosed(object sender, FormClosedEventArgs e)
        {
            clsConnection.CloseConnection();
        }

        private void btnCauhinh_Click(object sender, EventArgs e)
        {
            if (!CheckExistForm("frmCauhinh"))
            {
                frmCauhinh tt = new frmCauhinh();
                tt.MdiParent = this;
                tt.Show();
            }
            else ActiveChildForm("frmCauhinh");
        }
    }
}